package sunsun.xiaoli.jiarebang.utils;

import android.util.Log;
import com.google.gson.Gson;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.LogUtils;
import com.itboye.pondteam.volley.DESUtil;
import com.itboye.pondteam.volley.ParamDeal;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import static sunsun.xiaoli.jiarebang.utils.BaseConversion.*;

/**
 * @Author:Create by Mr.w
 * @Date:2018/11/27 14:19
 * @Description:
 */
public class ChatParams {

    private final String CHAT_TYPE_SEND_MESSNAGE = "01000";//发送消息
    private final String CHAT_TYPE_ENTER_LIVINGROOM = "01001";//进入房间
    private final String CHAT_TYPE_EXIT_LIVINGROOM = "01003";//进入房间
    private final String CHAT_TYPE_FORBID_USER = "01004";//禁言某人
    private final String CHAT_TYPE_RESUME_USER = "01006";//恢复某人发言
    /**
     * 01000	我发出去一条聊天信息（文本、表情）	用户昵称：nick
     * 用户id: uid
     * 聊天室id: room_id
     * 内容（控制不超过100个汉字,汉字、大小写英字母标点符号，emoji）：
     * text
     * 01001	我加入聊天室	用户id: uid
     * 昵称:nick
     * 聊天室id: room_id
     * 01003	我离开聊天室	用户id:uid
     * 聊天室id:room_id
     * 01004	[目前只有主播有权限]
     * 我要禁止某人或全员发言	forbid_uid = 0 表示房间聊天禁言
     * 被禁用户id:  forbid_uid
     * 房间id： room_id
     * 操作用户昵称：nick
     * 01006	我要恢复某人或全员发言	resume_uid = 0 表示房间聊天恢复
     * 被禁用户id:  resume_uid
     * 房间id： room_id
     * 操作用户昵称：nick
     */

    /**
     * 发送文本信息
     *
     * @param nick
     * @param uid
     * @param room_id
     * @param text
     */
    public byte[] sendParamsMap(String nick, String uid,String role, String room_id, String text) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("nick", nick);
        dataParamMap.put("uid", uid);
//        dataParamMap.put("to_uid", uid);
        dataParamMap.put("role", role);
        dataParamMap.put("room_id", room_id);
        dataParamMap.put("text", text);
        return rebuildParams(dataParamMap, CHAT_TYPE_SEND_MESSNAGE);
    }
    /**
     * 发送文本信息
     *
     * @param nick
     * @param uid
     * @param room_id
     * @param text
     */
    public byte[] sendParamsMap(String nick, String uid,String toUid,String role, String room_id, String text) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("nick", nick);
        dataParamMap.put("uid", uid);
        dataParamMap.put("to_uid", toUid);
        dataParamMap.put("role", role);
        dataParamMap.put("room_id", room_id);
        dataParamMap.put("text", text);
        return rebuildParams(dataParamMap, CHAT_TYPE_SEND_MESSNAGE);
    }
    /**
     * 进入房间
     *
     * @param nick
     * @param uid
     * @param room_id
     * @return
     */
    public byte[] enterLivingRoom(String nick, String uid, String room_id) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("nick", nick);
        dataParamMap.put("uid", uid);
        dataParamMap.put("room_id", room_id);
        return rebuildParams(dataParamMap, CHAT_TYPE_ENTER_LIVINGROOM);
    }

    /**
     * 退出房间
     *
     * @param uid
     * @param room_id
     * @return
     */
    public byte[] exitLivingRoom(String uid, String room_id) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("uid", uid);
        dataParamMap.put("room_id", room_id);
        return rebuildParams(dataParamMap, CHAT_TYPE_EXIT_LIVINGROOM);
    }

    /**
     * 禁言
     *
     * @param forbid_uid
     * @param room_id
     * @param nick
     * @return
     */
    public byte[] forbidUser(String forbid_uid, String room_id, String nick) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("nick", nick);
        dataParamMap.put("forbid_uid", forbid_uid);
        dataParamMap.put("room_id", room_id);
        return rebuildParams(dataParamMap, CHAT_TYPE_FORBID_USER);
    }

    /**
     * 恢复用户发言
     *
     * @param uid    被禁用户id
     * @param roomId 房间id
     * @param nick   操作用户昵称
     * @return
     */
    public byte[] resumeUser(String uid, String roomId, String nick) {
        HashMap<String, Object> dataParamMap = new HashMap<>();
        dataParamMap.put("nick", nick);
        dataParamMap.put("resume_uid", uid);
        dataParamMap.put("room_id", roomId);
        return rebuildParams(dataParamMap, CHAT_TYPE_RESUME_USER);
    }

    public byte[] rebuildParams(HashMap<String, Object> dataParamMap, String type) {
        String paramsJson = new Gson().toJson(dataParamMap);

        LogUtils.v(ChatTcp.TAG, "发送数据 before encode==" + paramsJson);
        String encodeData = null;//加密
        try {
            encodeData = new DESUtil().encode(paramsJson.getBytes("utf-8"), Const.CHAT_KEY);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int id = 5;
        long timestamp = System.currentTimeMillis() / 1000;
        String sign = new ParamDeal().getMD5Sign(id, type, encodeData, timestamp);

        HashMap<String, Object> pubMap = new HashMap<>();
        pubMap.put("id", id);
        pubMap.put("type", type);
        pubMap.put("timestamp", timestamp);
        pubMap.put("content", encodeData);
        pubMap.put("sign", sign);

        String jsonStr = new Gson().toJson(pubMap);
        LogUtils.v(ChatTcp.TAG, "发送数据==" + jsonStr);
        int length = jsonStr.length();
        LogUtils.v(ChatTcp.TAG, "data length" + length);

        int binary = 256;
        int q = (int) (length / Math.pow(binary, 3));
        int b = (int) (length / Math.pow(binary, 2)) - binary * q;
        int s = (int) (length / Math.pow(binary, 1)) - binary * binary * q - b * binary;
        int g = (int) (length / Math.pow(binary, 0) - binary * binary * binary * q - b * binary * binary - s * binary);

        byte[] by = new byte[4];
        by[3] = (byte) g;
        by[2] = (byte) s;
        by[1] = (byte) b;
        by[0] = (byte) q;
        LogUtils.v(ChatTcp.TAG, "256 进制换算后的四位  q:" + q + "" + " b:" + b + " s:" + s + " g:" + g);
        //重新拼凑要传给服务器的byte[]
        byte[] bytes = new byte[jsonStr.getBytes().length + 4];
        for (int i = 0; i < bytes.length; i++) {
            if (i <= 3) {
                bytes[i] = by[i];
            } else {
                bytes[i] = jsonStr.getBytes()[i - 4];
            }
        }
        LogUtils.v(ChatTcp.TAG, "byte to hex: " + bytesToHexFun1(bytes));
        return bytes;
//        char cc = (char) 1;
//        String str = (char) 0 + "" + (char) 0 + "" + (char) 0 + "" + cc + "A";
//        LogUtils.v(ChatTcp.TAG, "length  " + str+str + "  str:" + (str+str).length());
//        return str+str;
    }

}
